package by.grsu.epam.domain.user;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

@Entity
@Table(name="USERS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class User {
	
	protected Long id;
	protected String username;
	protected String firstname;
	protected String  lastname;
	protected String password;
	protected Boolean subscriberIsNotLocked;
	
	protected List<UserRole> roles;

	public User(){	
		roles = new ArrayList<UserRole>();
		subscriberIsNotLocked = true;
	}	

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	@Column(name = "lastname")
	public String getLastname() {
		return lastname;
	}

	public void setLastname(String secondname) {
		this.lastname = secondname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Type(type = "org.hibernate.type.NumericBooleanType")
	public Boolean getSubscriberIsNotLocked() {
		return subscriberIsNotLocked;
	}

	public void setSubscriberIsNotLocked(Boolean subscriberIsNotLocked) {
		this.subscriberIsNotLocked = subscriberIsNotLocked;
	}

	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "UserAuthorities", 
		joinColumns = {@JoinColumn(name = "userId")},
		inverseJoinColumns = {@JoinColumn(name = "authorityId")})
	public List<UserRole> getRoles() {
		return roles;
	}

	public void setRoles(List<UserRole> roles) {
		this.roles = roles;
	}
}
